package com.fenglingzff.reportstudent.web;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fenglingzff.reportstudent.constant.Constants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.*;

/**
 * 请求帮助类
 */
@ApiModel(value = "请求帮助类")
@Data
public class RequestHelper implements Serializable {

    /**
     * 排序的字段名
     */
    @ApiModelProperty(value = "排序的字段名")
    private String sortField;

    /**
     * 排序方式
     */
    @ApiModelProperty(value = "排序方式")
    private Boolean isAsc;
    /**
     * 查询信息
     */
//    @ApiModelProperty(value = "查询信息", hidden = true)
//    private String search;
    /**
     * 列表 页
     */
    @ApiModelProperty(value = "列表 页")
    private Integer pageNum;

    /**
     * 列表页size
     */
    @ApiModelProperty(value = "列表页size")
    private Integer pageSize;

    /**
     * 参数
     */
    @ApiModelProperty(value = "参数")
    private Map<String, Object> params;


    /**
     * 排序的字段名
     * "createTime:desc"
     */
    @ApiModelProperty(value = "排序的字段名", hidden = true)
    private List<String> sortFields;

    /**
     * 构建分页参数
     */
    public <T> Page<T> build() {
        Integer pageNum = ObjectUtil.defaultIfNull(getPageNum(), Constants.DEFAULT_PAGE_NUM);
        Integer pageSize = ObjectUtil.defaultIfNull(getPageSize(), Constants.DEFAULT_PAGE_SIZE);

        if (pageNum <= 0) {
            pageNum = Constants.DEFAULT_PAGE_NUM;
        }
        Page<T> page = new Page<>(pageNum, pageSize);
//        OrderItem orderItem = buildOrderItem(sortField, isAsc);
//        if (ObjectUtil.isNotNull(orderItem)) {
//            page.addOrder(orderItem);
//        }
//
//        List<OrderItem> orderItems = buildOrderList();
//        if (ObjectUtil.isNotEmpty(orderItems)) {
//            page.addOrder(orderItems);
//        }

        return page;
    }

    /**
     * 生成排序条件
     */
//    private OrderItem buildOrderItem(String sortField, Boolean isAsc) {
//        if (StringUtils.isBlank(sortField)) {
//            return null;
//        }
//
//        String orderBy = SqlUtil.escapeOrderBySql(sortField);
//        orderBy = StringUtils.toUnderScoreCase(orderBy);
//        return BooleanUtils.isNotFalse(isAsc) ? OrderItem.asc(orderBy) : OrderItem.desc(orderBy);
//    }
//
//    private List<OrderItem> buildOrderList() {
//        if (ObjectUtil.isEmpty(sortFields)) {
//            return null;
//        }
//        List<OrderItem> list = new ArrayList<>();
//        sortFields.forEach( string -> {
//            String[] split = string.split(":");
//            boolean isDesc = false;
//            if (split.length > 1) {
//                isDesc  = "desc".equals(split[1].trim()) || !BooleanUtils.toBoolean(split[1].trim());
//            }
//            list.add(buildOrderItem(split[0].trim(), !isDesc));
//        });
//        return list;
//    }

    /**
     * 添加查询参数
     */
    public  void addParams(String key, Object value) {
//        if (StringUtils.isNotEmpty(search) && !"{}".equals(search)) {
//            this.params = JsonUtils.parseMap(search);
//            this.search = null;
//        }
        if (ObjectUtils.isEmpty(params)) {
            this.params = new HashMap<>(8);
        }
        this.params.put(key,value);
    }

    /**
     * 删除查询参数
     */
    public void removeParams(String... keys) {
        if (ObjectUtils.isNotEmpty(this.params)) {
            for (String key : keys) {
                this.params.remove(key);
            }
        }

    }

//    public void searchToParams() {
//        if (StringUtils.isNotEmpty(search) && !"{}".equals(search)) {
//            if (ObjectUtil.isNotEmpty(this.params) ) {
//                Dict dict = JsonUtils.parseMap(search);
//                this.params.putAll(dict);
//
//            } else {
//                this.params = JsonUtils.parseMap(search);
//            }
//            this.search = null;
//        }
//    }

    public <T> T getParam(String key) {
//        this.searchToParams();

        if (ObjectUtil.isEmpty(this.params)) {
            return null;
        }
        Optional<Object> obj = Optional.ofNullable(this.params.get(key));
        if (obj.isPresent()) {
           return (T) obj.get();
        }
        return null;
    }

    public <T> T pop(String key){
//        this.searchToParams();
        if (ObjectUtil.isEmpty(this.params)) {
            return null;
        }
        Optional<Object> obj = Optional.ofNullable(this.params.get(key));
        if (obj.isPresent()) {
            this.params.remove(key);
            return (T)obj.get();
        }

        return null;
    }
}
